<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button v-auth="'manage:dept:add'" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
				<el-button v-auth="'manage:dept:remove'" type="danger" plain icon="el-icon-delete" :disabled="selection.length===0" @click="batchDel">删除</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.deptName" placeholder="部门名称" clearable></el-input>
					<sc-select v-model="search.status" dict="common_status" placeholder="请选择状态" style="width: 100%;" clearable></sc-select>
					<el-button v-auth="'manage:dept:list'" type="primary" icon="el-icon-search" @click="upSearch">搜索</el-button>
					<el-button @click="resetSearch">重置</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="dataList" row-key="id" @selection-change="selectionChange" hidePagination remoteSort remoteFilter>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="部门名称" prop="deptName" align="center" width="350"></el-table-column>
				<el-table-column label="排序" prop="orderNum" align="center" width="120"></el-table-column>
				<el-table-column label="状态" prop="status" align="center" width="150">
					<template #default="scope">
						<el-switch v-model="scope.row.status" @change="changeStatus($event, scope.row)" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="createTime" align="center" width="180" sortable='custom'></el-table-column>
				<el-table-column label="备注" prop="remark" align="center" min-width="100"></el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="160">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="tableShow(scope.row, scope.$index)">
								查看
							</el-button>
							<el-button v-auth="'manage:dept:edit'" text type="primary" size="small" @click="tableEdit(scope.row, scope.$index)">
								编辑
							</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="tableDel(scope.row, scope.$index)">
								<template #reference>
									<el-button v-auth="'manage:dept:remove'" text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="refreshTable" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './save.vue'

export default {
	name: 'dept',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			selection: [],
			search: {
				deptName: null,
				status: null,
			},
			dataList: this.$API.manage.dept.list
		}
	},
	mounted() {
	},
	methods: {
		// 添加
		add() {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open();
			})
		},
		// 批量删除
		batchDel() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项数据吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				const loading = this.$loading();
				const idList = [];
				this.selection.forEach(e => idList.push(e.id));
				const reqData = {idList: idList};
				this.$API.manage.dept.remove.delete(reqData).then(() => {
					this.$refs.table.refresh();
					this.$message.success("删除成功");
				})
				loading.close();
			})
		},
		// 查看
		tableShow(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row);
			})
		},
		// 编辑
		tableEdit(row) {
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row);
			})
		},
		// 删除
		tableDel(row) {
			const reqData = {idList: [row.id]};
			this.$API.manage.dept.remove.delete(reqData).then(() => {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			})
		},
		// 修改状态
		changeStatus(val, row) {
			row.$switch_status = true;
			const reqData = {id: row.id, status: row.status};
			this.$API.manage.dept.changeStatus.put(reqData).then(() => {
				delete row.$switch_status;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			})
		},
		// 搜索
		upSearch() {
			this.$refs.table.upData(this.search);
		},
		// 重置
		resetSearch() {
			this.search.deptName = null;
			this.search.status = null;
			this.$refs.table.upData(this.search);
		},
		// 表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		// 表格刷新
		refreshTable() {
			this.$refs.table.refresh();
		}
	}
}
</script>

<style>
</style>
